package org.springframework.beans.factory.support;

import cn.hutool.core.bean.BeanException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;

/**
 * @author 山顶洞人郑某 抽象bean工厂
 * @className AbstractBeanFactory.java
 * @date 2025 04 21
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    /**
     * 获取单例bean
     * @param name
     * @return
     * @throws BeanException
     */
    @Override
    public Object getBean(String name) throws BeanException {
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    /**
     * 获取bean的基本信息
     * @param beanName
     * @return bean的基本信息
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName);

    /**
     * 创建bean
     * @param beanName bean的名称
     * @param beanDefinition bean的基本信息
     * @return
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeanException;
}
